#!/bin/bash

########
# Description: Generating helm values for multi versions of app in k8s test env.
# Maintainer: sa@7moor.com
# 具体描述: 由于测试环境存在同一应用多版本并行运行的情况，所以需要按不通的命名空间同时运行多版本，已便于测试。
########

set -e

if [[ $CI_COMMIT_REF_NAME =~ 'release-one' ]]
then
    GENERATE_TEST_VALUE='test-one'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    # 替换APOLLO_CLUSTER
    sed -i 's/v-test-one/v-test-one/' test-values.yaml
    # 替换域名
    sed -i 's/m7_ingress_flag/true/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test1-v7.7moor.com/' test-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ 'release-two' ]]
then
    GENERATE_TEST_VALUE='test-two'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/m7_ingress_flag/true/' test-values.yaml
    sed -i 's/v-test-one/v-test-two/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test2-v7.7moor.com/' test-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ 'release-three' ]]
then
    GENERATE_TEST_VALUE='test-three'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/m7_ingress_flag/false/' test-values.yaml
    sed -i 's/v-test-one/v-test-three/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test3-v7.7moor.com/' test-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ 'release-four' ]]
then
    GENERATE_TEST_VALUE='test-four'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/m7_ingress_flag/false/' test-values.yaml
    sed -i 's/v-test-one/v-test-four/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test4-v7.7moor.com/' test-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ 'release-five' ]]
then
    GENERATE_TEST_VALUE='test-five'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/m7_ingress_flag/false/' test-values.yaml
    sed -i 's/v-test-one/v-test-five/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test5-v7.7moor.com/' test-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ 'release-six' ]]
then
    GENERATE_TEST_VALUE='test-six'
    echo "测试环境值: $GENERATE_TEST_VALUE"
    sed -i 's/m7_ingress_flag/false/' test-values.yaml
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-test-one/v-test-six/' test-values.yaml
    sed -i 's/test-v7.7moor.com/test6-v7.7moor.com/' test-values.yaml
# 研发环境
elif [[ $CI_COMMIT_REF_NAME =~ dev-one ]]
then
    GENERATE_TEST_VALUE='dev-one'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    # 替换APOLLO_CLUSTER
    sed -i 's/v-dev-one/v-dev-one/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev1-v7.7moor.com/' dev-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ dev-two ]]
then
    GENERATE_TEST_VALUE='dev-two'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-dev-one/v-dev-two/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev2-v7.7moor.com/' dev-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ dev-three ]]
then
    GENERATE_TEST_VALUE='dev-three'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-dev-one/v-dev-three/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev3-v7.7moor.com/' dev-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ dev-four ]]
then
    GENERATE_TEST_VALUE='dev-four'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-dev-one/v-dev-four/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev4-v7.7moor.com/' dev-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ dev-five ]]
then
    GENERATE_TEST_VALUE='dev-five'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-dev-one/v-dev-five/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev5-v7.7moor.com/' dev-values.yaml
elif [[ $CI_COMMIT_REF_NAME =~ dev-six ]]
then
    GENERATE_TEST_VALUE='dev-six'
    echo "开发环境值: $GENERATE_TEST_VALUE"
    echo "$GENERATE_TEST_VALUE" > GENERATE_TEST_VALUE
    sed -i 's/v-dev-one/v-dev-six/' dev-values.yaml
    # 替换域名
    sed -i 's/dev-v7.7moor.com/dev6-v7.7moor.com/' dev-values.yaml
else
    echo '测试环境/开发环境的分支名称不符合规范!'
    echo 'CD阶段将退出'
    exit 1
fi
